package servlets;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.DatabaseManager;
import managers.JSonManager;
import structures.User;

public class ModifyUserServlet extends HttpServlet {

	private static final long serialVersionUID = -4337723456854862474L;

		//method to insert
		protected void doPut(HttpServletRequest request, HttpServletResponse response){
			String name, surname, cap, city, address, jsonResult, payPalEmail, idUser;
			User user;
			JSonManager jSonManager;
			jSonManager = new JSonManager();
			name = request.getParameter("name");
			surname = request.getParameter("surname");
			cap = request.getParameter("cap");
			city = request.getParameter("city");
			address = request.getParameter("address");
			payPalEmail = request.getParameter("payPalEmail");
			idUser = request.getParameter("idUser");
			
			DatabaseManager databaseManager;
			databaseManager = new DatabaseManager();
			databaseManager.connect();
			user = databaseManager.modifyUser(name, surname, cap, city, address, payPalEmail, idUser);
			databaseManager.closeConnection();
			jsonResult = jSonManager.generateJSON(user);
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			try {
				//write the result as JSon
				response.getWriter().write(jsonResult);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	
}
